﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Properties;
using ScrumTable.Common.Threading;
using System.Threading;

#endregion

namespace ScrumTable.Common.Monitoring
{
    /// <summary>
    /// This class is used to forward import status to an import monitor.
    /// If an error occurs, the SaveImportMonitor ignores it and sets
    /// the has error flag.
    /// </summary>
    public class TimeLoggerImportMonitor : IDataImportMonitor
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDataImportMonitor _dataImportMonitor;
        private readonly IDictionary<string, TimeLogger<TimeLoggerImportMonitor>> _loggers
            = new Dictionary<string, TimeLogger<TimeLoggerImportMonitor>>();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new RangeImportMonitor instance with the specified data.
        /// </summary>
        public TimeLoggerImportMonitor(IDataImportMonitor monitor)
        {
            PreCondition.AssertNotNull(monitor, "monitor");

            _dataImportMonitor = monitor;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Calling this method determins that a new sub import progress has
        /// been started. This means that an additional progress bar should
        /// be displayed.
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the ReportProgress / EndSubProgress methods.</param>
        public void StartSubProgress(string identifier)
        {
            _loggers[identifier] = new TimeLogger<TimeLoggerImportMonitor>(Resources.LogTimeLoggerProgress, identifier);

            using (new TimeLogger<TimeLoggerImportMonitor>(true, Resources.LogTimeLoggerSubProgressStart, identifier))
            {
                _dataImportMonitor.StartSubProgress(identifier);
            }
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state in percent. The maximum is 100.</param>
        public void ReportProgress(string identifier, byte current)
        {
            using (new TimeLogger<TimeLoggerImportMonitor>(true, Resources.LogTimeLoggerReportProgress, identifier, current))
            {
                _dataImportMonitor.ReportProgress(identifier, current);
            }
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state.</param>
        /// <param name="max">Specifies the maximum.</param>
        public void ReportProgress(string identifier, int current, int max)
        {
            using (new TimeLogger<TimeLoggerImportMonitor>(true, Resources.LogTimeLoggerReportProgressMax, identifier, current))
            {
                _dataImportMonitor.ReportProgress(identifier, current, max);
            }
        }

        /// <summary>
        /// Reports the end of a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the StartSubProgress / ReportProgress methods.</param>
        /// <param name="e">This argument will contain an exception if the progress has been aborted.</param>
        public void EndSubProgress(string identifier, Exception e)
        {
            using (new TimeLogger<TimeLoggerImportMonitor>(true, Resources.LogTimeLoggerEndSubProgress, identifier, e))
            {
                _dataImportMonitor.EndSubProgress(identifier, e);
            }

            // log process duration
            if (_loggers.ContainsKey(identifier))
            {
                _loggers[identifier].Dispose();
                _loggers.Remove(identifier);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}